package com.tititi.auth;


import com.tititi.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限拦截
 *
 * @author tang
 */
@Slf4j
@Component
public class PermissionInterceptor extends HandlerInterceptorAdapter {

	@Resource
	private IUserService userService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		
		if (!(handler instanceof HandlerMethod)) {
			return super.preHandle(request, response, handler);
		}

		log.info("----intercept success");
		/*
		// if need login
		boolean needLogin = true;
		HandlerMethod method = (HandlerMethod)handler;
		AuthLimit authLimit = method.getMethodAnnotation(AuthLimit.class);
		if (authLimit!=null && "LOGIN".equals(authLimit.value())) {
			needLogin = true;
		}

		if (needLogin) {
			XxlJobUser loginUser = loginService.ifLogin(request, response);
			if (loginUser == null) {
				response.sendRedirect(request.getContextPath() + "/toLogin");
				//request.getRequestDispatcher("/toLogin").forward(request, response);
				return false;
			}
			if (needAdminuser && loginUser.getRole()!=1) {
				throw new RuntimeException(I18nUtil.getString("system_permission_limit"));
			}
			request.setAttribute(LoginService.LOGIN_IDENTITY_KEY, loginUser);
		}
		*/
		return super.preHandle(request, response, handler);
	}
	
}
